/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
 *    6, April, 2011. Created
 */

package portfolio.dbHost.dataBases.dbTable;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import portfolio.dbHost.dataBases.DBase;
import portfolio.dbHost.dataBases.dbTable.Table;
import portfolio.dbHost.dataBases.dbTable.tblColumn.OdbcColumn;

/**
 *
 * @author aca ldero
 */
public class OdbcTable extends Table
{
    public OdbcTable (DBase dBaseParam, String sTableParam) throws Exception
    {
        super (dBaseParam, sTableParam);
    }
    public OdbcTable (DBase dBaseParam) throws Exception
    {
        super (dBaseParam);
    }

    @Override
    public Vector[] describeAll(Connection connectParam, String sDBNameParam, String sTable) throws Exception
    {
        if (connectParam == null)
            throw new Exception ("null connection.");
        if (sDBNameParam == null)
            throw new Exception ("null database name.");
        if (sTable == null)
            throw new Exception ("null table name.");

        Vector [] outVector = null;
        try {
            Statement statement = connectParam.createStatement ();
            statement.execute ("USE " + sDBNameParam + ";");

            String sNativeStmt = connectParam.nativeSQL ("EXEC sp_columns @table_name = " + sTable +";");
            ResultSet resultSet = statement.executeQuery (sNativeStmt);
            if (!resultSet.next ()) {
                throw new Exception ("describeTable method: resultSet = null.");
            }

            ResultSetMetaData rsmd = resultSet.getMetaData ();
            int iColCountTmp = rsmd.getColumnCount();

            if (iColCountTmp == 0) {
                throw new Exception ("null iColumnCount.");
            }
            outVector = new Vector [iColCountTmp];
            for (int iIdx = 0; iIdx < iColCountTmp; iIdx++) {
                outVector [iIdx] = new Vector ();
            }

            do {
                 String sField = resultSet.getString (4); //1);
                outVector [1 -1].addElement (sField);
                String sType = resultSet.getString (6);
                outVector [2 -1].addElement (sType);
                int iNull = resultSet.getInt (11);
                String sNull  = (iNull == 1)? " NULL": " NOT NULL";
                outVector [3 -1].addElement (sNull);
//                String sKey = resultSet.getString (4);
//                outVector [4 -1].addElement (sKey);
//                String sDefault = resultSet.getString (5);
//                outVector [5 -1].addElement (sDefault);
//                String sExtra = resultSet.getString (6);
//                outVector [6 -1].addElement (sExtra);
            } while (resultSet.next ());
        }
        catch (SQLException sqlex) {
            throw new Exception (sqlex);
        }

        return (outVector);
    }

    @Override
    public void buildColumns() throws Exception, SQLException {
//        Vector [] vTemp = this.describeAll ();
        Vector [] vTemp = this.describeAll(connection, sDBName, sTableName);
        vColumns = vTemp [0];
        iColumnCount = vTemp [0].size();
        if (iColumnCount != 0) {
            columns = new OdbcColumn [iColumnCount];
            //System.out.println ("sTable " + iColumnCount);
            for (int iIdx = 0; iIdx < iColumnCount; iIdx++) {
//                columns [iIdx] = new ColumnFactory ().getColumnDBType(Host.DB_TYPE_ODBC, this);
                columns [iIdx] = new OdbcColumn (this);
                columns [iIdx].setField (vTemp[0].get(iIdx).toString());
                ( columns [iIdx]).setType  (vTemp[1].get(iIdx).toString());
                ( columns [iIdx]).setNull  (vTemp[2].get(iIdx).toString());
//0j0                ((MysqlColumn) columns [iIdx]).setKey   (vTemp[3].get(iIdx).toString());
            }
        }

        return;
    }
}
